package com.atguigu.eduservice.controller;


import com.atguigu.commonutil.R;
import com.atguigu.eduservice.entity.Teacher;
import com.atguigu.eduservice.service.TeacherService;
import com.atguigu.eduservice.vo.TeacherQuery;
import com.atguigu.servicebase.exceptionHandler.MyException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-02-02
 */
@RestController
@RequestMapping("/eduservice/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @GetMapping("findAll")
    public R findAll() {
//        try {
//            int i = 1 / 0;
//        } catch (Exception e) {
//            throw new MyException(666, "自定义异常");
//        }
        return R.ok().data("items", teacherService.list(null));
    }

    @DeleteMapping("{id}")
    public R deleteById(@PathVariable("id") String id) {
        boolean flag = teacherService.removeById(id);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation("分页查询讲师")
    @PostMapping("/pageList/{page}/{limit}")
    public R page(@ApiParam(name = "page", value = "当前页码", required = true) @PathVariable("page") Long page,
                  @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable("limit") Long limit) {
        Page<Teacher> pageParam = new Page<>(page, limit);
        teacherService.page(pageParam, null);
        List<Teacher> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

    @ApiOperation("带查询条件的分页查询")
    @PostMapping("/pageTeacherCondition/{page}/{limit}")
    public R pageTeacherCondition(@ApiParam(name = "page", value = "当前页码", required = true) @PathVariable("page") Long page,
                                  @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable("limit") Long limit,
                                  @ApiParam(name = "condition", value = "查询条件", required = false) @RequestBody(required = false) TeacherQuery teacherQuery) {
        Page<Teacher> pageParam = new Page<>(page, limit);
        teacherService.pageQuery(pageParam, teacherQuery);
        List<Teacher> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }


    @ApiOperation("新增讲师")
    @PostMapping("/save")
    public R save(@RequestBody Teacher teacher) {
        boolean flag = teacherService.save(teacher);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //根据id查询,用于信息回显
    @ApiModelProperty(value = "根据id查询")
    @GetMapping("/getById/{id}")
    public R getById(@PathVariable String id) {
        Teacher teacher = teacherService.getById(id);
        return R.ok().data("item", teacher);
    }

    //修改讲师
    @ApiModelProperty(value = "修改讲师")
    @PostMapping("/updateById")
    public R updateById(@RequestBody Teacher teacher) {
        boolean flag = teacherService.updateById(teacher);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }


}

